﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
/// <summary>
/// 修改前
/// </summary>
public class Elevator :SimpleElectricalEquipment
{
    [SerializeField] Transform plane;
    [SerializeField] Rigidbody2D Elevat;

    [SerializeField] private float speed = 2;
    [SerializeField] private float targetAccuracy = 0.1f;
    Vector2 target;
    bool move = false;
    float dir_y = 0;
    float dir_x = 0;

    float TargetDistance_Y { get { return Mathf.Abs(plane.position.y - target.y); } }
    float TargetDistance_X { get { return Mathf.Abs(plane.position.x - target.x); } }
    public bool Arrive_Y { get { return TargetDistance_Y <= targetAccuracy; } }
    public bool Arrive_X { get { return TargetDistance_X <= targetAccuracy; } }
    public List<IElevatorBtn> elevatorBtns  = new List<IElevatorBtn>();


    private void FixedUpdate()
    {
        if (move)
        {
            if (!Arrive_Y)
            {
                DecisionMove_Y();
            }
            else if (!Arrive_X)
            {
                DecisionMove_X();
            }
            if (Arrive_X && Arrive_Y)
            {
                foreach (var item in elevatorBtns)
                {
                    item.Off();
                }
                move = false;
            }
        }
        else
        {
            Elevat.constraints = RigidbodyConstraints2D.FreezeAll;
        }
    }

    public void MoveTo(Vector2 target_o, IElevatorBtn elevatorBtn)
    {
        if (!isOpen)
        {
            return;
        }

        if (target_o.x!=-99)
        {
            target = new Vector2(target_o.x, plane.position.y);
        }
        else
        {
            target = new Vector2(plane.position.x, plane.position.y);
        }
        if (target_o.y!=-99)
        {
            target = new Vector2(target.x, target_o.y);
        }
        
        foreach (var item in elevatorBtns)
        {
            if (item!=elevatorBtn)
            {
                item.Off();
            }
        }
        move = true;
    }

    private void DecisionMove_Y()
    {
        if (plane.transform.position.y > target.y)
        {
            dir_y = -1;
        }
        else
        {
            dir_y = 1;
        }
        Elevat.constraints = RigidbodyConstraints2D.FreezePositionX;
        Elevat.freezeRotation = true;
        Elevat.MovePosition(new Vector2(Elevat.position.x, Elevat.position.y + dir_y * speed * Time.fixedDeltaTime));
    }
    
    private void DecisionMove_X()
    {
        
        if (plane.transform.position.x > target.x)
        {
            dir_x = -1;
        }
        else
        {
            dir_x = 1;
        }
        Elevat.constraints = RigidbodyConstraints2D.FreezePositionY;
        Elevat.freezeRotation = true;
        Elevat.MovePosition(new Vector2(Elevat.position.x + dir_x * speed * Time.fixedDeltaTime, Elevat.position.y));
    }

}


/*
 using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
/// <summary>
/// 修改前
/// </summary>
public class Elevator :SimpleElectricalEquipment
{
    [SerializeField] Transform plane;
    [SerializeField] Transform Elevat;

    [SerializeField] private float speed = 2;
    [SerializeField] private float targetAccuracy = 0.1f;
    Vector2 target;
    bool move = false;
    float dir_y = 0;
    float dir_x = 0;

    float TargetDistance_Y { get { return Mathf.Abs(plane.position.y - target.y); } }
    float TargetDistance_X { get { return Mathf.Abs(plane.position.x - target.x); } }
    public bool Arrive_Y { get { return TargetDistance_Y <= targetAccuracy; } }
    public bool Arrive_X { get { return TargetDistance_X <= targetAccuracy; } }
    public List<IElevatorBtn> elevatorBtns  = new List<IElevatorBtn>();


    private void Update()
    {
        if (move)
        {
            if (!Arrive_Y)
            {
                DecisionMove_Y();
            }
            else if (!Arrive_X)
            {
                DecisionMove_X();
            }
            if (Arrive_X && Arrive_Y)
            {
                Debug.Log("End");
                foreach (var item in elevatorBtns)
                {
                    item.Off();
                }
                move = false;
            }
        }
    }

    public void MoveTo(Vector2 target_o, IElevatorBtn elevatorBtn)
    {

        if (target_o.x!=-99)
        {
            target = new Vector2(target_o.x, plane.position.y);
        }
        else
        {
            target = new Vector2(plane.position.x, plane.position.y);
        }
        if (target_o.y!=-99)
        {
            target = new Vector2(target.x, target_o.y);
        }
        
        foreach (var item in elevatorBtns)
        {
            if (item!=elevatorBtn)
            {
                item.Off();
            }
        }
        move = true;
    }

    private void DecisionMove_Y()
    {
        if (plane.transform.position.y > target.y)
        {
            dir_y = -1;
        }
        else
        {
            dir_y = 1;
        }
        Elevat.position = new Vector3(Elevat.position.x, Elevat.position.y + dir_y * speed * Time.deltaTime, Elevat.position.z);
    }
    
    private void DecisionMove_X()
    {
        
        if (plane.transform.position.x > target.x)
        {
            dir_x = -1;
        }
        else
        {
            dir_x = 1;
        }
        Elevat.position = new Vector3(Elevat.position.x + dir_x * speed * Time.deltaTime, Elevat.position.y , Elevat.position.z);
    }

}

     */
